package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.view.NoScrollListView;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.SubGoodAdapter;
import com.xfly.app.bean.KcBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SubListActivity extends BaseMvpActivity {

    TextView addaddress;
    TitleBarView title_bar;
    List<KcBean> list = new ArrayList<>();
    SubGoodAdapter subGoodAdapter;
    NoScrollListView listview;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    EditText name,phone;
    TextView sub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_subgood);
    }

    @Override
    public void initViews() {

        addaddress = findViewById(R.id.addaddress);
        listview = findViewById(R.id.listview);
        title_bar = findViewById(R.id.title_bar);
        name = findViewById(R.id.name);
        phone = findViewById(R.id.phone);
        sub = findViewById(R.id.sub);
        subGoodAdapter = new SubGoodAdapter(getContext(), list);
        listview.setAdapter(subGoodAdapter);
        subGoodAdapter.setClick(new SubGoodAdapter.Click() {
            @Override
            public void Click(int position) {
                list.get(position).num = new BigDecimal(list.get(position).num).add(new BigDecimal("1")).toEngineeringString();
                subGoodAdapter.pushData(list);
            }

            @Override
            public void Click2(int position) {
                if(list.get(position).num.equals("1")){
                    return;
                }
                list.get(position).num = new BigDecimal(list.get(position).num).subtract(new BigDecimal("1")).toEngineeringString();
                subGoodAdapter.pushData(list);
            }
        });
    }

    @Override
    public void initEvents() {
        title_bar.setText("录入线下订单");
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        addaddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), SubGoodActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sub();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        list.add((KcBean) data.getSerializableExtra("bean"));
        subGoodAdapter.pushData(list);
    }

    private void sub(){
        if(list.size()==0){
            showMsg("请至少选择一个商品");
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("name",name.getText().toString());
        map.put("phone",phone.getText().toString());
        StringBuffer stringBuffer = new StringBuffer();
        for(KcBean kcBean:list){
            stringBuffer.append(kcBean.id);
            stringBuffer.append(","+kcBean.id);
            stringBuffer.append(","+kcBean.num);
            stringBuffer.append("-");
        }
        map.put("goods",stringBuffer.toString().substring(0,stringBuffer.toString().length()-1));
        apiDataRepository.subgood(map, new ApiNetResponse<Object>(getContext()) {
            @Override
            public void onSuccess(Object data) {
                showMsg("上传成功");
                finish();
            }
        });
    }
}
